package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.MyUtil;
import com.ie.entity.CartEntity;
import com.ie.entity.FocusEntity;
import com.ie.mapper.admin.GoodsMapper;
import com.ie.entity.GoodsEntity;
import com.ie.mapper.admin.GoodsTypeMapper;
import com.ie.service.before.FocusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javax.management.Query.eq;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService{
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    private FocusService focusService;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        Integer currentPage = goodsEntity.getCurrentPage();
        if(currentPage == null || currentPage < 0){
            currentPage = 1;
        }
        IPage<GoodsEntity> ipage = new Page<>(currentPage, 10);
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodsTypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if(goodsTypeId != null && goodsTypeId != 0){
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsTypeId);
        }
        if(gname != null && !gname.isEmpty()){
            lambdaQueryWrapper.like(GoodsEntity::getGname, gname);
        }
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(ipage, lambdaQueryWrapper);
        HashMap<String, Object> myres = new HashMap<>();
        myres.put("AllGoods", page.getRecords());
        myres.put("Total", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<Map<String, Object>> addGoods(GoodsEntity goodsEntity) {
        byte[] logoFile = goodsEntity.getLogoFile();
        if (logoFile != null && logoFile.length > 0) {
            //Vue前端静态资源路径, 这个是要修改的
            String path = "D:\\zr\\zongheshixun\\ebusiness-vueQD\\ebusiness-vueQD\\ebusiness-vue\\src\\assets";
            String path_linux = "/var/www/ebusiness-vue/src/assets;";
            String fileName = MyUtil.getNewFileName(goodsEntity.getFileName());
            File filePath = new File(path + File.separator + fileName);
            if (!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                fileOutputStream.write(logoFile);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            goodsEntity.setGpicture(fileName);
        }
        if("add".equals(goodsEntity.getAct())){
            boolean result = save(goodsEntity);
            if (result) {
                return ResponseResult.getMessageResult(null, "A001");
            }
            else {
                return ResponseResult.getMessageResult(null, "A002");
            }
        }
        else{
            boolean result = updateById(goodsEntity);
            if (result) {
                return ResponseResult.getMessageResult(null, "A003");
            }
            else {
                return ResponseResult.getMessageResult(null, "A004");
            }
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> deleteGoods(GoodsEntity goodsEntity) {
//        long res1 = cartService.getId();
//        long res2 = focusService.*;
//        long res3 = orderdetailService.*;
        long res1 = 0;
        long res2 = 0;
        long res3 = 0;
        if (res1 > 0 || res2 > 0 || res3 > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        else if (removeById(goodsEntity)) {
            return ResponseResult.getMessageResult(null, "A002");
        }
        else {
            return ResponseResult.getMessageResult(null, "A003");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> updateGoods(GoodsEntity goodsEntity) {
        return null;
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> listAdv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId).last("limit 5").list();
        return ResponseResult.getSuccessResult(listAdv);
    }

    @Override
    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {

        //return ResponseResult.getSuccessResult(goodsMapper.selectById(goodsEntity.getId()));

        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodsTypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if(goodsTypeId != null && goodsTypeId > 0){
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsTypeId);
        }
        if(gname != null && !gname.isEmpty()){
            String trimgname = gname.trim();
            lambdaQueryWrapper.like(GoodsEntity::getGname, trimgname);
        }
        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        List<GoodsEntity> listindex = list(lambdaQueryWrapper);
        return ResponseResult.getSuccessResult(listindex);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCartGoods(CartEntity cartEntity) {
        List<Map<String, Object>> cartGoods = goodsMapper.myCartGoods(cartEntity.getBusertableId());
        return ResponseResult.getSuccessResult(cartGoods);
    }

    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        IPage<GoodsEntity> ipage = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<FocusEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FocusEntity::getBusertableId, goodsEntity.getBusertableId());
        IPage<GoodsEntity> page = goodsMapper.iPageMyFocusGoods(ipage, lambdaQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("allGoods", page.getRecords());
        map.put("total", page.getTotal());
        return ResponseResult.getSuccessResult(map);
    }

    @Override
    public Integer getGoodsStoreById(GoodsEntity goodsEntity) {
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(GoodsEntity::getId, goodsEntity.getId())
                .select(GoodsEntity::getGstore);
        GoodsMapper goodsMapper = getBaseMapper();
        Integer store = goodsMapper.selectOne(lambdaQueryWrapper).getGstore();
        if(store != null || store != 0)
        {
            return store;
        }
        return null;
    }

    @Override
    public ResponseResult<Map<String, Object>> batchUdateGoods(List<GoodsEntity> list) {
        for (int i = 0; i < list.size(); i++) {
            updateById(list.get(i));
        }
        return ResponseResult.getMessageResult(null, "A001");
    }


}
